package com.example.Lezedata.ui.activity.common;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.Lezedata.R;
import com.example.Lezedata.ui.activity.baseClass.GBBaseVc;
import com.example.Lezedata.ui.activity.login.LoginVc;

public class TransitionPage extends GBBaseVc {

    @Override
    protected int getLayoutId() {
        return R.layout.activity_transition_page;
    }

    private ImageView Trans_imageView;
    private TextView Trans_Title;
    private TextView Trans_Tip;
    private TextView Trans_button;

    // resId
    public static String Key_image = "Key_image";
    // string
    public static String Key_title = "Key_title";
    // string
    public static String Key_tip = "Key_tip";

    int time = 60;
    final Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            countdown();
            refreshButton();
            handler.postDelayed(this,1000);
        }
    };

    @Override
    protected void viewDidLoad() {
        super.viewDidLoad();
        findView();
        setData();
    }

    void findView(){
        Trans_imageView = findViewById(R.id.Trans_imageView);
        Trans_Title = findViewById(R.id.Trans_Title);
        Trans_Tip = findViewById(R.id.Trans_Tip);
        Trans_button = findViewById(R.id.Trans_button);
        Trans_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                back();
            }
        });
        refreshButton();
        runnable.run();
    }

    void setData(){
        int resId = getIntent().getIntExtra(Key_image,0);
        String title = getIntent().getStringExtra(Key_title);
        String tip = getIntent().getStringExtra(Key_tip);
        Trans_imageView.setImageResource(resId);
        Trans_Title.setText(title);
        Trans_Tip.setText(tip);
        
        if (title == null){
            LinearLayout layout = (LinearLayout)Trans_Title.getParent();
            layout.removeView(Trans_Title);
        }
        if (tip == null){
            LinearLayout layout = (LinearLayout)Trans_Tip.getParent();
            layout.removeView(Trans_Tip);
        }
    }

    void countdown(){
        time --;
        if (time <= 0){
            finish();
        }
    }

    void refreshButton(){
        if (time <= 0){
            return;
        }
        Trans_button.setText(String.format("返回(%d)",time));
    }

    void back(){
        finishActivityToClass(LoginVc.class);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacks(runnable);
    }
}